﻿using System.Data.Entity;
using System.Linq;

namespace SAFTCopyToDB
{
    public class EFCodeFirstSAFT : DbContext
    {
        public EFCodeFirstSAFT() { }

        public EFCodeFirstSAFT(string connectionString)
            : base(connectionString)
        { }

        public DbSet<Header> Header { get; set; }

        public DbSet<SourceDocumentsSalesInvoices> SalesInvoices { get; set; }

        public DbSet<SourceDocumentsSalesInvoicesInvoice> Invoices { get; set; }

        public DbSet<SourceDocumentsSalesInvoicesInvoiceLine> InvoiceLines { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var properties = new[]
            {
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoice>().Property(invoice => invoice.NetTotal),
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoice>().Property(invoice => invoice.TaxPayable),
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoice>().Property(invoice => invoice.GrossTotal),
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoice>().Property(invoice => invoice.CurrencyCreditAmount),
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoice>().Property(invoice => invoice.CurrencyDebitAmount),
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoice>().Property(invoice => invoice.SettlementAmount),
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoiceLine>().Property(line => line.Quantity),
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoiceLine>().Property(line => line.UnitPrice),
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoiceLine>().Property(line => line.CreditAmount),
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoiceLine>().Property(line => line.DebitAmount),
                modelBuilder.Entity<SourceDocumentsSalesInvoicesInvoiceLine>().Property(line => line.SettlementAmount),
            };
            properties.ToList().ForEach(property =>
            {
                property.HasPrecision(18, 6);
            });

            base.OnModelCreating(modelBuilder);
        }
    }
}
